package unclevenn.cloudmusic.until;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.util.List;
import java.util.Map;

public class BeansToMap {
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (null != bean) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

    public static <T> List<Map<String, Object>> objectsToMaps(List<T> object) {
        List<Map<String, Object>> list = Lists.newArrayList();
        if (null != object) {
            Map<String, Object> map;
            for (T bean : object) {
                map = beanToMap(bean);
                list.add(map);
            }
        }
        return list;
    }
}
